<?php

namespace App\Http\Controllers\Admin\Service;

use App\Http\Controllers\Controller;
use App\Http\Requests\service\DatabaseRequest;
use App\Models\service\DatabaseModel;
use Illuminate\Http\Request;

class Database extends Controller
{
    use DatabaseRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $name = $request->input('name', '');
        $type_id = $request->input('type_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = DatabaseModel::query()->with(['create_admin', 'type']);

        if($name){
            $query = $query->where('name', 'like', '%'.$name.'%');
        }

        if($type_id){
            $query = $query->where('type_id', $type_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'name' => $item->name,
                'type_name' => $item->type ? $item->type->name : '',
                'file' => $item->file,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $find = DatabaseModel::where('name', $params['name'])->first();
        if($find){
            return $this->error('名称已存在');
        }

        $model = new DatabaseModel();
        $model->name = $params['name'];
        $model->type_id = $params['type_id'];
        $model->file = $params['file'];
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = DatabaseModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        if($model->name != $params['name']){
            $find = DatabaseModel::where('name', $params['name'])->first();
            if($find){
                return $this->error('名称已存在');
            }
        }

        $model->name = $params['name'];
        $model->type_id = $params['type_id'];
        $model->file = $params['file'];
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = DatabaseModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

}
